package com.wanmait.wisdom.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wisdom.entity.House;
import com.wanmait.wisdom.manage.service.ManageHouseService;
import com.wanmait.wisdom.mapper.HouseMapper;
import com.wanmait.wisdom.util.AjaxResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)

public class ManageHouseServiceImpl extends ServiceImpl<HouseMapper, House> implements ManageHouseService {

    @Resource
    private HouseMapper houseMapper;

    @Override
    public List<House> selectHouseById(Integer id) {
        LambdaQueryWrapper<House> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(House::getUnitId,id);
        // 根据id查询房屋信息
        return houseMapper.selectList(queryWrapper);
    }

    @Override
    public AjaxResult houseByid(Integer id) {

        return AjaxResult.success("查询成功").put("house",houseMapper.selectById(id));
    }


    @Override
    public Integer getUnsoldCount() {
        return houseMapper.getUnsoldCount();
    }

    @Override
    public Integer getRentOutCount() {
        return houseMapper.getRentOutCount();
    }

    @Override
    public Integer getSoldOutCount() {
        return houseMapper.getSoldOutCount();
    }

    public AjaxResult listPage(House house, Integer pageNum, Integer pageSize) {
//        Page<House> page = new Page<>(pageNum,pageSize);
//        Page<House> housePage = houseMapper.selectPageList(page,null);
//        return AjaxResult.success().put("housePage",housePage);
        Page<House> page = new Page<>(pageNum,pageSize);
        Page<House> housePage = null;

        if(Objects.nonNull(house)){
            housePage = houseMapper.selectPageList(page,house);
        }else{
            housePage = houseMapper.selectPageList(page,null);
        }
        return AjaxResult.success().put("housePage",housePage);
    }

    @Override
    public AjaxResult searchPage(House house, Integer pageNum, Integer pageSize) {
        Page<House> page = new Page<>(pageNum,pageSize);
        Page<House> housePage = null;

        if(house != null){
            housePage = houseMapper.selectPageList(page,house);
        }else{
             housePage = houseMapper.selectPageList(page,null);
        }

        return AjaxResult.success().put("housePage",housePage);
    }

    @Override
    public AjaxResult HouseAdd(House house) {
        if(house.getId()==null){
           save(house);
            return AjaxResult.success("添加成功");
        }
       else {
           houseMapper.updateById(house);
            return AjaxResult.success("修改成功");
        }

    }

    @Override
    public AjaxResult blurNumber(House house) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("unit_id", house.getUnitId());
        wrapper.eq("floor",house.getFloor());
        wrapper.eq("house_number" ,house.getHouseNumber());
        House house1 = houseMapper.selectOne(wrapper);
        return AjaxResult.success("查找成功").put("house",house1);
    }

    @Override
    public List<House> selectByUnitId(House house) {
        return houseMapper.selectByUnitId(house.getUnitId());
    }


}
